const chalk = require('chalk')
const assert = require('assert')
const ebuild = require('./build')
const startDev = require('./dev')
const checkOption = require('./checkOption')
module.exports = ({ build, core, deploy }, options) => {
  options = checkOption(options)
  //  注册dev命令
  core.registerCommend('dev-cs', {
    description: '客户端开发模式',
    usage: 'vea dev-cs [target]',
    detail: `
            Examples:
                ${chalk.gray('# vea dev-cs  dev')}
                运行dev客户端开发环境
                `.trim()
  }, (argv) => {
    const [target = ''] = argv
    process.env.VEA_ENV = target // 全局变量
    assert(core.commonds.build, '模块de-csv命令不存在')
    process.env.NODE_ENV = 'development'
    core.run('dev', argv, {
      before: fbuild => setBuildRender(fbuild, options).setIsOpenBrowser(false),
      after:
        ({ server }) => {
          console.log(chalk.green(' dev render编译完成'))
          setBuildMain(build, options)
            .once('onBuildSuccess', () => {
              console.log(chalk.green(' dev main编译完成'))
              startDev()
            }).startBuild(process.env.NODE_ENV)
        }
    })
  })

  //  注册build命令
  core.registerCommend('build-cs', {
    description: '项目打包到客户端',
    usage: 'vea build-cs [target]',
    detail: `
            Examples:
                ${chalk.gray('# vea build-cs dev')}
                打包dev环境客户端
                `.trim()
  }, (argv) => {
    const [target = ''] = argv
    process.env.VEA_ENV = target // 全局变量
    process.env.NODE_ENV = 'production'
    assert(core.commonds.build, '模块build命令不存在')
    core.run('build', argv, {
      before: fbuild => setBuildRender(fbuild, options),
      after:
        () => {
          console.log(chalk.green('render编译完成'))
          setBuildMain(build, options).once('onBuildSuccess', () => {
            console.log(chalk.green('main编译完成'))
            ebuild()
          }).startBuild()
        }
    })
  })
}

/**
 * 设置renderBuild配置
 * @param build
 * @param options
 * @returns {*}
 */
function setBuildRender (build, options) {
  return build
    .setOutputPath(options.output)
    .setTarget('electron-renderer')
    .setLibraryTarget('commonjs2')
    .setNode({
      __dirname: process.env.NODE_ENV !== 'production',
      __filename: process.env.NODE_ENV !== 'production'
    })
    .setDefined({ // 设置electron 变量
      'process.env.IS_ELECTRON': true
    })
}

/**
 * 设置mainBuild配置
 * @param build
 * @param options
 * @returns {*}
 */
function setBuildMain (build, options) {
  return (() => {
    const babel = build.config.babel
    const index = babel.presets.findIndex(item => {
      const [presetPath = ''] = item
      return presetPath.indexOf('preset-env') > -1
    })
    babel.presets[index][1] = {
      targets: {
        node: '7'
      }
    }
    return build.setBabel(babel)
  })()
    .setEntry({ 'index': options.entry })
    .setTarget('electron-main')
    .setClear(false)// 不清除目标目录
    .setHtmlTemplate('@vea-no-template')// 设置不使用html文件
    .setHash(false) // 不启用hash
}
